/**
 * File:   StatisticsTab.java
 * Author: Gvennet
 */
package gui.tabs;

import dht.node.DHTStatistics;
import dht.node.DHTStatisticsEventListener;
import gui.actions.ExportStatisticsAction;
import gui.main.DHTStatisticsTableModel;

import javax.swing.*;
import java.awt.*;

/**
 * todo write javadoc
 */
public final class StatisticsTab implements DHTStatisticsEventListener
{

	private JComponent component;

	private DHTStatisticsTableModel tableModel = new DHTStatisticsTableModel();

	public StatisticsTab(DHTStatistics statistics, JFrame frame)
	{
		component = new JPanel(new BorderLayout());

		JButton exportButton = new JButton(new ExportStatisticsAction(statistics, frame));

		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		JPanel exportPanel = new JPanel(flowLayout);
		exportPanel.add(exportButton);

		JTable statisticsTable = new JTable(tableModel);
		statisticsTable.setFocusTraversalPolicyProvider(true);
		JScrollPane scrollPane = new JScrollPane(statisticsTable);

		component.add(BorderLayout.CENTER, scrollPane);
		component.add(BorderLayout.SOUTH, exportPanel);

		component.setVisible(true);
	}

	public JComponent getComponent()
	{
		return component;
	}

	@Override
	public void statisticsUpdate(final int[] actualStatisticsData)
	{
		if (SwingUtilities.isEventDispatchThread())
		{

			tableModel.addRow(actualStatisticsData);
			component.repaint();
		}
		else
		{
			SwingUtilities.invokeLater(new Runnable()
			{

				@Override
				public void run()
				{
					statisticsUpdate(actualStatisticsData);
				}
			});
		}
	}
}
